import 'package:demo/views/group/group_invite_view.dart';
import 'package:demo/views/group/group_qrcode_view.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:demo/views/group/group_kicked_view.dart';
import 'package:demo/views/group/group_name_view.dart';
import 'package:demo/views/group/group_notice_view.dart';
import 'package:demo/views/group/group_details_controller.dart';
import 'package:demo/tools/tools_sqlite.dart';
import 'package:demo/component/component_common.dart';
import 'package:demo/component/component_line.dart';
import 'package:demo/component/component_avatar.dart';

// 群聊详情
class GroupDetailsView extends GetView<GroupDetailsController> {
  // 路由地址
  static const String routeName = '/group_details';
  const GroupDetailsView({super.key});

  @override
  Widget build(BuildContext context) {
    Get.lazyPut(() => GroupDetailsController());
    return GetBuilder<GroupDetailsController>(
      builder: (builder) {
        ChatGroup chatGroup = controller.chatGroup;

        return Scaffold(
          resizeToAvoidBottomInset: false,
          appBar: AppBar(
            title: const Text('群详情'),
          ),
          body: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 4),
            child: ListView(
              children: [
                _buildHeader(chatGroup.portrait),
                ComponentLineRow(
                  '群名称',
                  value: chatGroup.groupName,
                  onTap: () {
                    Get.toNamed(GroupNameView.routeName);
                  },
                ),
                ComponentLineRow(
                  '群聊ID',
                  value: chatGroup.groupNo,
                  arrow: false,
                  onLongPress: () {
                    Clipboard.setData(ClipboardData(text: chatGroup.groupNo));
                    EasyLoading.showToast('文本已复制');
                  },
                ),
                ComponentLineRow(
                  '群二维码',
                  widget: const Icon(Icons.qr_code),
                  onTap: () {
                    Get.toNamed(
                      GroupQrCodeView.routeName,
                      arguments: chatGroup,
                    );
                  },
                  divider: false,
                ),
                ComponentCommon.border(),
                ComponentLineContent(
                  '群聊公告',
                  chatGroup.notice,
                  onTap: () {
                    Get.toNamed(GroupNoticeView.routeName);
                  },
                  divider: false,
                ),
                ComponentCommon.border(),
                ComponentLineRow(
                  '邀请好友',
                  subtitle: '邀请好友加入群聊',
                  onTap: () {
                    Get.toNamed(
                      GroupInviteView.routeName,
                    );
                  },
                ),
                ComponentLineRow(
                  '移除成员',
                  subtitle: '指定成员移出群聊',
                  onTap: () {
                    Get.toNamed(
                      GroupKickedView.routeName,
                    );
                  },
                  divider: false,
                ),
                ComponentCommon.border(),
                ComponentLineRow(
                  '消息免打扰',
                  subtitle: '开启后可继续接收消息，但不提醒',
                  widget: Switch(
                    value: chatGroup.memberDisturb == 'Y',
                    onChanged: (bool value) {
                      // 提交
                      controller.setDisturb(value ? 'Y' : 'N');
                    },
                  ),
                  arrow: false,
                ),
                ComponentLineRow(
                  '消息置顶',
                  subtitle: '开启后消息将显示在列表顶端',
                  widget: Switch(
                    value: chatGroup.memberTop == 'Y',
                    onChanged: (bool value) {
                      controller.setTop(value ? 'Y' : 'N');
                    },
                  ),
                  arrow: false,
                  divider: false,
                ),
                ComponentCommon.border(),
                ComponentLineCenter(
                  '退出群聊',
                  color: Colors.red,
                  onTap: () {
                    _logout(context);
                  },
                  divider: false,
                ),
                ComponentCommon.border(),
                ComponentLineCenter(
                  '清空消息',
                  divider: false,
                  color: Colors.red,
                  onTap: () {
                    _clearHis(context);
                  },
                ),
                ComponentCommon.border(),
              ],
            ),
          ),
        );
      },
    );
  }

  // 顶部头像
  _buildHeader(String portrait) {
    return ComponentLineRow(
      '群头像',
      widget: ComponentCommon.showAvatar(
        portrait,
        size: 50,
      ),
      hight: 8,
      onTap: () {
        ComponentAvatar.image(
          onTap: (value) {
            // 提交
            controller.editPortrait(value);
          },
        );
      },
    );
  }

  // 退出群聊
  _logout(BuildContext context) {
    showCupertinoDialog(
      context: context,
      builder: (BuildContext context) {
        return CupertinoAlertDialog(
          content: const Text(
            '确认要退出当前群聊吗？',
            style: TextStyle(
              fontSize: 16,
            ),
          ),
          actions: [
            CupertinoDialogAction(
              child: const Text('取消'),
              onPressed: () {
                Get.back();
              },
            ),
            CupertinoDialogAction(
              child: const Text('确认'),
              onPressed: () {
                Get.back();
                // 提交
                controller.logout();
              },
            ),
          ],
        );
      },
    );
  }

  // 清空消息
  _clearHis(BuildContext context) {
    showCupertinoDialog(
      context: context,
      builder: (BuildContext context) {
        return CupertinoAlertDialog(
          content: const Text(
            '确认清空消息吗？',
            style: TextStyle(
              fontSize: 16,
            ),
          ),
          actions: [
            CupertinoDialogAction(
              child: const Text('取消'),
              onPressed: () {
                Get.back();
              },
            ),
            CupertinoDialogAction(
              child: const Text('确认'),
              onPressed: () {
                Get.back();
                controller.clearHis();
              },
            ),
          ],
        );
      },
    );
  }
}
